package com.zlj.request;

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Request获取请求参数：请求参数是由客户端发送给服务器的！有可能是在请求体中(post),也可能是在URL之后(GET)
 * 
 * 请求参数：有一个参数一个值的，也有一个参数多个值的
 * 
 * > ** String getParameter(String name)：获取指定名称的请求参数值，适用于单值请求参数
 * > String[] getParameterValues(String name):获取指定名称的请求参数值，适用于多值请求参数
 * > Enumeration<String> getParameterNames():获取所有请求参数名称
 * > ** Map<String,String[]> getParameterMap():获取所有的请求参数，其中key为参数名，value为参数值
 */
@WebServlet("/RequestDServlet")
public class RequestDServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          System.out.println("用户名：" + request.getParameter("username"));
          System.out.println("密    码：" + request.getParameter("password"));
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String username = request.getParameter("username");
          String password = request.getParameter("password");
          String[] hobbies = request.getParameterValues("hobbies");
          System.out.println("用户名：" + username + "; 密码：" + password + "; 爱好:" + Arrays.toString(hobbies));
          
          /*
           * 获取所有请求参数名称
           */
          Enumeration<String> names = request.getParameterNames();
          while(names.hasMoreElements()){
        	  System.out.println(names.nextElement());
          }
          
          /*
           * 获取所有的请求参数，其中key为参数名，value为参数值
           */
          Map<String,String[]> key = request.getParameterMap();
          for(String name : key.keySet()){
        	  System.out.println("参数名：" + name + "; 参数值:" + Arrays.toString(key.get(name)));
          }
          
	}
}
